//
//  Translated.m
//  Translator
//
//  Created by ebeerman on 9/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Translated.h"



@implementation Translated

@synthesize outputBox;
@synthesize translatedText;
@synthesize originalText;
@synthesize language;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    outputBox.text = translatedText;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)sendInAppSMS:(id)sender {
    MFMessageComposeViewController *messageVC = [[[MFMessageComposeViewController alloc] init] autorelease];
    
    if([MFMessageComposeViewController canSendText]) {
        NSString *body = [NSString stringWithFormat:@"The following text was translated to %@, how awesome is that? It started off as:<br/>%@<br/>And became:<br/>%@<br/>After it was translated!", language, originalText, translatedText];
        messageVC.body = body;
        messageVC.messageComposeDelegate = self;
        [self presentModalViewController:messageVC animated:YES];
    }
    
}

-(IBAction)sendEmail:(id)sender{
    if ([MFMailComposeViewController canSendMail]){
        MFMailComposeViewController *composeVC = [[[MFMailComposeViewController alloc] init] autorelease];
        composeVC.mailComposeDelegate = self;
    
        [composeVC setSubject:@"Read this hilarious translation!"];
        NSString *body = [NSString stringWithFormat:@"The following text was translated to %@, how awesome is that? It started off as:<br/>%@<br/>And became:<br/>%@<br/>After it was translated!", language, originalText, translatedText];
    
        [composeVC setMessageBody:body isHTML:YES];
    
        if (composeVC != nil){
            [self presentModalViewController:composeVC animated:YES];
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Email" message: @"No email capabilities exist on this device! Sorry!"
                                                       delegate:self cancelButtonTitle:@"Oh no! I guess I'll just have to buy a new iPhone..." otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    UIAlertView *alert;
	switch (result) {
		case MessageComposeResultCancelled:
			break;
		case MessageComposeResultFailed:
            alert = [[UIAlertView alloc] initWithTitle:@"MyApp" 
                                               message:@"Unknown Error"
                                            delegate:self 
                                        cancelButtonTitle:@"OK" 
                                        otherButtonTitles: nil];
			[alert show];
			[alert release];
			break;
		case MessageComposeResultSent:
            
			break;
		default:
			break;
	}
	[self dismissModalViewControllerAnimated:YES];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
    UIAlertView *alert;
	switch (result) {
		case MFMailComposeResultCancelled:
			break;
		case MFMailComposeResultFailed:
            alert = [[UIAlertView alloc] initWithTitle:@"MyApp" 
                                               message:@"Unknown Error"
                                              delegate:self 
                                     cancelButtonTitle:@"OK" 
                                     otherButtonTitles: nil];
			[alert show];
			[alert release];
			break;
		case MFMailComposeResultSent:
            
			break;
        case MFMailComposeResultSaved:
            break;
		default:
			break;
	}
	[self dismissModalViewControllerAnimated:YES];
}


@end
